package com.qxzn.module.home.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.AlbumPictureBean;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.module.home.R;
import com.qxzn.module.home.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AlbumPictureGridAdapter extends RecyclerView.Adapter<AlbumPictureGridAdapter.ViewHolder> {

    private Activity activity;
    private List<AlbumPictureBean.ResultheadBean> dataList;
    private AlbumPictureBean.ResultheadBean item;
    private MyInterface.AlbumPictureClickListener pictureClickListener;
    private int parentPosition;
    private View.OnClickListener itemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (pictureClickListener != null) {
                pictureClickListener.onAlbumPictureClickSuccess(v, parentPosition, (int) v.getTag());
            }
        }
    };

    public AlbumPictureGridAdapter(Activity activity, List dataList) {
        this.activity = activity;
        this.dataList = dataList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_album_grid, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        item = dataList.get(position);
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(itemClick);
        Glide.with(activity).load(HttpRequest.BASE_URL + item.getPhoto()).apply(new RequestOptions().placeholder(R.mipmap.logo).error(R.mipmap.logo).centerCrop()).into(holder.ivPicture);
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    public void setPictureClickListener(MyInterface.AlbumPictureClickListener pictureClickListener) {
        this.pictureClickListener = pictureClickListener;
    }

    public void setParentPosition(int parentPosition) {
        this.parentPosition = parentPosition;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.iv_picture)
        ImageView ivPicture;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
